import AsyncStorage from '@react-native-community/async-storage';

export default class DeviceStorage {
  /**
   * 获取
   * @param key
   * @returns
   */
  static get(key) {
    return AsyncStorage.getItem(key).then(value => {
      return JSON.parse(value);
    });
  }

  /**
   * 保存
   * @param key
   * @param value
   * @returns {*}
   */
  static set(key, value) {
    return AsyncStorage.setItem(key, JSON.stringify(value));
  }

  /**
   * 更新
   * @param key
   * @param value
   * @returns
   */
  static update(key, value) {
    return DeviceStorage.get(key).then(item => {
      value =
        typeof value === 'string' ? value : Object.assign({}, item, value);
      return DeviceStorage.set(key, value);
    });
  }

  /**
   * 删除
   * @param key
   * @returns {*}
   */
  static delete(key) {
    return AsyncStorage.removeItem(key);
  }
}
